import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:webview_flutter/webview_flutter.dart';

import '../constants/colors.dart';

class HiWebview extends StatefulWidget {
  final String? url;
  final String? title;
  final bool? hideAppBar;
  final bool? backForbid;

  const HiWebview(
      {super.key,
      this.url,
      this.title,
      this.hideAppBar,
      this.backForbid});

  @override
  State<HiWebview> createState() => _HiWebviewState();
}

class _HiWebviewState extends State<HiWebview> {
  String? url;
  late WebViewController controller;

  _backButton(Color backButtonColor) {
    return GestureDetector(
      onTap: (){
        Get.back();
      },
      child: Container(
        margin: const EdgeInsets.only(left: 10),
        child: Icon(
          Icons.arrow_back,
          color: backButtonColor,
        ),
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    url = widget.url;
    if (url != null && url!.contains('ctrip.com')) {
      url = url!.replaceAll("http://", "https://");
    }
    _initWebViewController();
  }

  @override
  Widget build(BuildContext context) {
    Color backButtonColor = Colors.white;

    return WillPopScope(
        onWillPop: () async {
          if (await controller.canGoBack()) {
            controller.goBack();
          } else {
            if (context.mounted) Get.back();
          }
          return true;
        },
        child: Scaffold(
          body: Column(
            children: [
              _appBar(
                  AppColors.primaryColor, backButtonColor),
              Expanded(
                  child: WebViewWidget(
                controller: controller,
              ))
            ],
          ),
        ));
  }

  void _initWebViewController() {
    controller = WebViewController()
      ..setJavaScriptMode(JavaScriptMode.unrestricted)
      ..setBackgroundColor(const Color(0x00000000))
      ..setNavigationDelegate(NavigationDelegate(
          onProgress: (int progress) {
            debugPrint('progress:$progress');
          },
          onPageStarted: (String url) {},
          onPageFinished: (String url) {
            _handleBackForbid();
          },
          onWebResourceError: (WebResourceError error) {},
          onNavigationRequest: (NavigationRequest request) {
            return NavigationDecision.navigate;
          }))
      ..loadRequest(Uri.parse(url!));
  }

  void _handleBackForbid() {
    const jsStr =
        "var element = document.querySelector('.animationComponent.rn-view'); element.style.display = 'none';";
    if(widget.backForbid??false){
      debugPrint(jsStr);
      controller.runJavaScript(jsStr);
    }
  }

  _appBar(Color backgroundColor, Color backButtonColor) {
    double top = MediaQuery.of(context).padding.top;
    if (widget.hideAppBar ?? false) {
      return Container(color: backgroundColor, height: top);
    }

    return Container(
      color: backgroundColor,
      padding: EdgeInsets.fromLTRB(0, top, 0, 10),
      child: FractionallySizedBox(
        widthFactor: 1,
        child: Stack(
          children: [_backButton(Colors.white), _title(backButtonColor)],
        ),
      ),
    );
  }



  _title(Color backButtonColor) {
    return Positioned(
      left: 0,
      right: 0,
      child: Center(
        child: Text(
          widget.title ?? "",
          style: TextStyle(color: backButtonColor, fontSize: 20),
        ),
      ),
    );
  }
}
